﻿using Avalonia.Collections;
using Avalonia.Styling;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using P2P.Client.UI.Features.CustomTheme;
using P2P.Client.UI.Services;
using P2P.Client.UI.Utilities;
using SukiUI.Controls;
using P2P.Client.UI.Features;
using SukiUI.Models;
using System.Collections.Generic;
using System.Linq;
using SukiUI;

namespace P2P.Client.UI;

public partial class MainViewModel : ObservableObject
{
    public IAvaloniaReadOnlyList<PageBase> Pages { get; }

    public IAvaloniaReadOnlyList<SukiColorTheme> Themes { get; }

    [ObservableProperty] private ThemeVariant _baseTheme;
    [ObservableProperty] private bool _animationsEnabled;
    [ObservableProperty] private PageBase? _activePage;
    [ObservableProperty] private bool _windowLocked = false;
    [ObservableProperty] private bool _titleBarVisible = true;

    private readonly SukiTheme _theme;

    public MainViewModel(IEnumerable<PageBase> pages, PageNavigationService nav)
    {
        Pages = new AvaloniaList<PageBase>(pages.OrderBy(x => x.Index).ThenBy(x => x.DisplayName));
        _theme = SukiTheme.GetInstance();
        nav.NavigationRequested += t =>
        {
            var page = Pages.FirstOrDefault(x => x.GetType() == t);
            if (page is null || ActivePage?.GetType() == t) return;
            ActivePage = page;
        };
        Themes = _theme.ColorThemes;
        BaseTheme = _theme.ActiveBaseTheme;
        _theme.OnBaseThemeChanged += async variant =>
        {
            BaseTheme = variant;
            await SukiHost.ShowToast("Successfully Changed Theme", $"Changed Theme To {variant}");
        };
        _theme.OnColorThemeChanged += async theme =>
            await SukiHost.ShowToast("Successfully Changed Color", $"Changed Color To {theme.DisplayName}.");
        _theme.OnBackgroundAnimationChanged +=
            value => AnimationsEnabled = value;
    }

    [RelayCommand]
    private void ToggleAnimations()
    {
        AnimationsEnabled = !AnimationsEnabled;
    }

    [RelayCommand]
    private void ToggleBaseTheme() =>
        _theme.SwitchBaseTheme();

    public void ChangeTheme(SukiColorTheme theme) =>
        _theme.ChangeColorTheme(theme);

    [RelayCommand]
    private void CreateCustomTheme() =>
        SukiHost.ShowDialog(new CustomThemeDialogViewModel(_theme), allowBackgroundClose: true);

    [RelayCommand]
    private void ToggleWindowLock()
    {
        WindowLocked = !WindowLocked;
        SukiHost.ShowToast(
            $"Window {(WindowLocked ? "Locked" : "Unlocked")}",
            $"Window has been {(WindowLocked ? "locked" : "unlocked")}.");
    }

    [RelayCommand]
    private void ToggleTitleBar()
    {
        TitleBarVisible = !TitleBarVisible;
        SukiHost.ShowToast(
            $"Title Bar {(TitleBarVisible ? "Visible" : "Hidden")}",
            $"Window title bar has been {(TitleBarVisible ? "shown" : "hidden")}.");
    }

    [RelayCommand]
    private void OpenURL(string url) => UrlUtilities.OpenURL(url);
}